package de.derhess.projects.pkr.controller
{
	import de.derhess.projects.pkr.models.PKRBaseData;
	
	import mx.events.PropertyChangeEvent;

	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  ControllerPKRBaseData
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	public class ControllerPKRBaseData
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		private static var BOUNDARY_VALUES:Array = new Array(300,1500,5000,10000,25000,50000, 200000, 500000, 99999999);
		private static var STEPS:Array = new Array(300, 300, 500,1000,3000,5000,15000, 30000, 50000, 99999999);
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var model:PKRBaseData;
		
		private var rvgBaseValues:Array;
		private var rvgIncreases:Array;
		
		private var gkgBaseValues:Array;
		private var gkgIncreases:Array;
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function ControllerPKRBaseData(data:PKRBaseData)
		{
			model = data;
			
			rvgBaseValues = new Array(25,25,105,301,486,686,1046,1816,2996);
			rvgIncreases = new Array(0,20,28,37,40,72,77,118,150);
			
			gkgBaseValues = new Array(25,25,65,121,196,311,456,1456,2956);
			gkgIncreases = new Array(0,10,8,15,23,29,100,150,150);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		public function setValueInDispute(value:Number):void
		{
			this.model.valueInDispute = value;
			this.model.lawyerFee = calcRVGfee(value);
			this.model.courtFee = calcGKGfee(value);
		}
		
		public function setNumberClients(value:int):void
		{
			this.model.amountClients = value;
		}
		
		public function setNumberEnemies(value:int):void
		{
			this.model.amountEnemyClients = value;	
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function toString():String 
		{
			return "[ControllerPKRBaseData] : model: " + model.toString();	
		}
		
		// Berechnung Gebühr nach RVG §13 --> Rechtsanwalt Vergütungsgebühr
		private function calcRVGfee(disputeValue:Number):Number 
		{
			var i:int= 0;
			var stepCounter:int = 0;
			
			if (disputeValue>0){
				while( (BOUNDARY_VALUES[i] < disputeValue)  &&  (i <= BOUNDARY_VALUES.length))
				{
					i++;
				}
				stepCounter = int( ( (disputeValue - BOUNDARY_VALUES[Math.max(i-1,0)]) -0.001 ) / STEPS[i] ) + 1;
				return (stepCounter * rvgIncreases[i] + rvgBaseValues[i]);
			} else {
				return 0;
			}
		}
		
		// Berechnung Gebühr nach GKG §34 --> Gerichtskostengebühr
		private function calcGKGfee (disputeValue:Number):Number
		{
			var i:int = 0;
			var stepCounter:int = 0;
			
			if (disputeValue > 0){
				while( (BOUNDARY_VALUES[i]<disputeValue) && (i <= BOUNDARY_VALUES.length)){
					i++;
				}
				stepCounter = int( ( (disputeValue - BOUNDARY_VALUES[Math.max(i-1,0)]) - 0.001)/ STEPS[i] ) + 1;
				return (stepCounter * gkgIncreases[i] + gkgBaseValues[i]);
			} else {
				return 0;
			}	
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		public function setStartValues(valueInDispute:Number = 0,numberOfClients:int = 1, numberOfEnemies:int = 1):void
		{
			this.model.valueInDispute = valueInDispute;
			this.model.amountClients = numberOfClients;
			this.model.amountEnemyClients = numberOfEnemies;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}